#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>

#define FOR(i,a,n) for(int i=a,_n(n);i<_n;++i)
#define REP(i,n) FOR(i,0,n)

using namespace std;

int GetAns(int s,int f,int t,int m)
{
    if(s==f) return t;
    int now = t%(m-1)+1;
    if(t/(m-1)&1)
    {
        s = m - s + 1;
        f = m - f + 1;
       // now = m - now + 1;
    }
   // printf("%d %d %d %d now = %d\n",s,f,t,m,now);
    int ans = t;
    if(s>=now)
    {
        ans += s - now;
        if(f>=s) ans += f - s;
        else ans += m - s + m - f;
       // printf("%d %d %d\n",s-now,m-s+m-f,ans);
    }
    else
    {
        ans += m - s  + m - now;
        if(f<=s) ans += s - f;
        else ans += s - 1 + f - 1;
    }
    return ans;
}

int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        REP(i,n)
        {
            int s,f,t;
            cin>>s>>f>>t;
            cout<<GetAns(s,f,t,m)<<endl;
        }
    }
	return 0;
}
